﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using DevExpress.XtraEditors;
using DevExpress.XtraBars;
using TERP.WaterClient.Common;
using System.Collections;
using TERP.WaterClient.Views.Common;
using TERP.WaterClient.Views.Auth;
using TERP.WaterClient.Entities.Auth;

namespace TERP.WaterClient.Views
{
	public partial class FrmMain : XtraForm
	{
		#region  Declaration
		//admin - dealer
		public const int MDI_ID_ADM_PORT_TYPE = 3;
		public const int MDI_ID_ADM_PORT = 4;
		public const int MDI_ID_ADM_AREA = 5;
		public const int MDI_ID_ADM_DEALER = 6;

		// Water

		public const int MDI_ID_WATER_CUSTOMER = 201;
		public const int MDI_ID_WATER_STOCKOUT = 202;
		public const int MDI_ID_WATER_UPDATE_PRICE = 203;
		public const int MDI_ID_WATER_REPORT_STOCKOUT = 204;
		public const int MDI_ID_WATER_REPORT_TOTAL_STOCKOUT = 205;
		public const int MDI_ID_WATER_STOCKOUT_LIST = 206;
		public const int MDI_ID_WATER_MNG_BILL = 207;
		public const int MDI_ID_WATER_MNG_RECEIPT = 208;
		public const int MDI_ID_WATER_MNG_STOCKOUT = 209;
		public const int MDI_ID_WATER_REPORT_REPORT_SUM = 210;
		public const int MDI_ID_WATER_REPORT_TOTAL_DEBIT = 211;
		
		
		//Services
		public const int MDI_ID_ITS_CUSTOMER = 201;
        public const int MDI_ID_ITS_CUSTOMERGROUP = 202;
        public const int MDI_ID_ITS_CUSTOMERGROUP_LIST = 203;
		public const int MDI_ID_ITS_TOUR_LIST = 205;
        public const int MDI_ID_ITS_TOUR = 206;
        public const int MDI_ID_OPERATION_TOUR_LIST = 204;
		public const int MDI_ID_SERVICES_MST_PART = 23;
		public const int MDI_ID_SERVICES_QUOTATION = 24;


		//
		//auth
		public const int MDI_ID_AUTH_MNG_USER = 401;
		public const int MDI_ID_AUTH_MNG_GROUP = 402;
		public const int MDI_ID_AUTH_MAP_USER_GROUP = 403;
		public const int MDI_ID_AUTH_MAP_GROUP_FUNCTION = 404;
		//Test
		public const int MDI_ID_TEST = 100;
		private FrmLogin _frmLogin;
		private bool _bShowLogin = false;
		#endregion


		/// <summary>
		/// 
		/// </summary>
		/// <param name="frmLogin"></param>
		public FrmMain(FrmLogin frmLogin)
		{
			InitializeComponent();
			this._frmLogin = frmLogin;
			this.SetLanguage();
			this.InitMenu();
			this.SetMenuAuth();
			//
            this.Icon = EmbeddedRes.Instance.AppIcon;
			this.WindowState = FormWindowState.Maximized;
		}


		private void SetLanguage()
		{
			this.Text = "MTV Binh Duong - QL Xuất nước";
			barSubFile.Caption = "Hệ thống";
			barBtnLogout.Caption = "Đăng xuất";
			barSubAdmin.Caption = "Quản trị";
			//barSubSales.Caption = "Bán hàng";
			barSubHelp.Caption = "Trợ giúp";
			barBtnTutor.Caption = "Hướng dẫn";
			barBtnAbout.Caption = "Giới thiệu";
			barSubStyle.Caption = "Kiểu giao diện";
			barSubWindows.Caption = "Cửa sổ";


			barSubReport.Caption = "Báo cáo";
			//
			barBtnTabMdi.Caption = "Sử dụng tab";
			barBtnExit.Caption = "Thoát";

			barSubSales.Caption = "Quản lý xuất";
			barOperations.Caption = "Thu - chi";
			barOperations.Visibility = BarItemVisibility.Never;

			//
			barOperations.Tag = MenuFunction.Menu_Service;
            //barSubAdmin.Tag = MenuFunction.Menu_Sys;
			barProductManagement.Tag = MenuFunction.Menu_Inventory;

			barSubFile.Tag = MenuFunction.Menu_File;
			barSubHelp.Tag = MenuFunction.Menu_Help;
			barSubWindows.Tag = MenuFunction.Menu_Windows;
            barSubAdmin.Tag = MenuFunction.Menu_Admin;
			barSubStyle.Tag = MenuFunction.Menu_Style;
			barSubSales.Tag = MenuFunction.Menu_Sales;

		}

		private void InitMenu()
		{
			//style
			foreach (DictionaryEntry dic in LayoutStyle.Instance.GetHtbStyle())
			{
				BarCheckItem item = new BarCheckItem(barManager,
					LayoutStyle.Instance.GetDefaultStyle().Equals(dic.Key));
				item.Caption = (string)dic.Key;
				item.Tag = dic.Value;
				barSubStyle.AddItem(item);
				item.ItemClick += new ItemClickEventHandler(OnSkinClick);
			}
			//tab
			barBtnTabMdi.Down = true;
			this.barBtnTabMdi_ItemClick(null, null);
			//
			

            //Admin
          
           


			this.CreateMdiMenu(barSubAdmin, "", MDI_ID_WATER_CUSTOMER, "Danh sách khách hàng");
			this.CreateMdiMenu(barSubAdmin, "", MDI_ID_WATER_UPDATE_PRICE, "Cập nhật giá bán");
			//this.CreateMdiMenu(barSubAdmin, "", MDI_ID_CAR_USER, "Quản lý người dùng", true);

			//this.CreateMdiMenu(barSubAdmin, "", MDI_ID_AUTH_BACKUP_DATA, "Sao lưu dữ liệu", true);
			//this.CreateMdiMenu(barSubAdmin, "", MDI_ID_AUTH_RESTORE_DATA, "Phục hồi dữ liệu");

			this.CreateMdiMenu(barSubAdmin, MenuFunction.Menu_Admin_MagUser, MDI_ID_AUTH_MNG_USER, "Quản lý người dùng", true);
			this.CreateMdiMenu(barSubAdmin, MenuFunction.Menu_Admin_MagGroup, MDI_ID_AUTH_MNG_GROUP, "Quản lý nhóm");
			this.CreateMdiMenu(barSubAdmin, MenuFunction.Menu_Admin_AssigGroupUser, MDI_ID_AUTH_MAP_USER_GROUP, "Gán nhóm, người dùng");
			this.CreateMdiMenu(barSubAdmin, MenuFunction.Menu_Admin_AssigGroupFunction, MDI_ID_AUTH_MAP_GROUP_FUNCTION, "Gán nhóm, chức năng");

			//
			this.CreateMdiMenu(barSubSales, "", MDI_ID_WATER_STOCKOUT_LIST, "Quản lý xuất nước");
			this.CreateMdiMenu(barSubSales, "", MDI_ID_WATER_STOCKOUT, "Xuất nước cho khách hàng");
			this.CreateMdiMenu(barSubSales, "", MDI_ID_WATER_MNG_STOCKOUT, "Tổng hợp xuất nước", true);

			//Bill
			this.CreateMdiMenu(barOperations, "", MDI_ID_WATER_MNG_BILL, "Lập phiếu chi");
			//this.CreateMdiMenu(barBillReceipt, "", MDI_ID_WATER_MNG_BILL, "Quản lý phiếu chi");
			this.CreateMdiMenu(barOperations, "", MDI_ID_WATER_MNG_RECEIPT, "Lập phiếu thu", true);
			//this.CreateMdiMenu(barBillReceipt, "", MDI_ID_WATER_MNG_RECEIPT, "Quản lý phiếu thu");

			//Report
			this.CreateMdiMenu(barSubReport, "", MDI_ID_WATER_REPORT_STOCKOUT, "Báo cáo chứng từ ghi sổ");
			this.CreateMdiMenu(barSubReport, "", MDI_ID_WATER_REPORT_TOTAL_STOCKOUT, "Báo cáo tổng hợp khách hàng");
			this.CreateMdiMenu(barSubReport, "", MDI_ID_WATER_REPORT_REPORT_SUM, "Tổng kết Nhập - Xuất - Doanh thu", true);
			this.CreateMdiMenu(barSubReport, "", MDI_ID_WATER_REPORT_TOTAL_STOCKOUT, "Tổng kết thu, chi");
			this.CreateMdiMenu(barSubReport, "", MDI_ID_WATER_REPORT_TOTAL_DEBIT, "Tổng kết công nợ khách hàng");
			//this.CreateMdiMenu(barReport, "", MDI_ID_WATER_REPORT_TOTAL_STOCKOUT, "Tổng kết nợ vỏ khách hàng");

            

            
		}

		private void OnSkinClick(object sender, ItemClickEventArgs e)
		{
			foreach (BarItemLink itemLink in barSubStyle.ItemLinks)
			{
				if (itemLink.Item is BarCheckItem)
				{
					((BarCheckItem)itemLink.Item).Checked = false;
				}
			}
			string skinName = (string)e.Item.Tag;
			DevExpress.LookAndFeel.UserLookAndFeel.Default.SetSkinStyle(skinName);
			((BarCheckItem)e.Item).Checked = true;
		}

		private void barBtnExit_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
		{
			_bShowLogin = false;
			this.Close();
			//_frmLogin.Close();
		}

		private void barBtnLogout_ItemClick(object sender, ItemClickEventArgs e)
		{
			_bShowLogin = true;
			this.Close();
			//_frmLogin.Show();
		}

		private void FrmMain_FormClosed(object sender, FormClosedEventArgs e)
		{
			if (_bShowLogin)
				_frmLogin.Show();
			else
				_frmLogin.Close();
		}

		private void barBtnTutor_ItemClick(object sender, ItemClickEventArgs e)
		{
		}

		private bool IsTabMDI
		{
			get { return barBtnTabMdi.Down; }
		}

		private void barBtnTabMdi_ItemClick(object sender, ItemClickEventArgs e)
		{
			tabbedMdiManager.MdiParent = null;
			tabbedMdiManager.MdiParent = IsTabMDI ? this : null;
			barBtnCascade.Visibility = barBtnTileHorziontal.Visibility =
				barBtnTileVertical.Visibility = IsTabMDI ? BarItemVisibility.Never : BarItemVisibility.Always;
		}

		private void barBtnCascade_ItemClick(object sender, ItemClickEventArgs e)
		{
			this.LayoutMdi(MdiLayout.Cascade);
		}

		private void barBtnTileHorziontal_ItemClick(object sender, ItemClickEventArgs e)
		{
			this.LayoutMdi(MdiLayout.TileHorizontal);
		}

		private void barBtnTileVertical_ItemClick(object sender, ItemClickEventArgs e)
		{
			this.LayoutMdi(MdiLayout.TileVertical);
		}

		/// <summary>
		/// 
		/// </summary>
		/// <param name="barParent"></param>
		/// <param name="iMdiId"></param>
		/// <param name="menuTitle"></param>
		/// <param name="bStartGroup"></param>
		public void CreateMdiMenu(BarSubItem barParent, string functionCode, int iMdiId, string menuTitle)
		{
			CreateMdiMenu(barParent, functionCode, iMdiId, menuTitle, false);
		}

		/// <summary>
		/// 
		/// </summary>
		/// <param name="barParent"></param>
		/// <param name="iMdiId"></param>
		/// <param name="menuTitle"></param>
		/// <param name="bStartGroup"></param>
		public void CreateMdiMenu(BarSubItem barParent, string functionCode, int iMdiId, string menuTitle, bool bStartGroup)
		{
			MyBarButtonItem item = new MyBarButtonItem(barManager, menuTitle);
			item.MdiId = iMdiId;
			item.Tag = functionCode;
			barParent.LinksPersistInfo.AddRange(new DevExpress.XtraBars.LinkPersistInfo[] {
            new DevExpress.XtraBars.LinkPersistInfo(item, bStartGroup)});
			//barParent.AddItem(item);
			item.ItemClick += new ItemClickEventHandler(OnMdiClick);
		}

		private void OnMdiClick(object sender, ItemClickEventArgs e)
		{
			if (!(e.Item is MyBarButtonItem))
				return;
			MyBarButtonItem item = (MyBarButtonItem)e.Item;
			int mdiId = item.MdiId;
			//check
			bool found = false;
			foreach (Form frm in this.MdiChildren)
			{
				if (frm is FrmMdiBase)
				{
					int tmp = ((FrmMdiBase)frm).MdiId;
					if (tmp == mdiId)
					{
						frm.Activate();
						found = true;
					}
				}
			}
			//
			if (!found)
			{
				FrmMdiBase frmBase = GetMdiForm(mdiId);
				if (frmBase != null)
				{
					frmBase.Text = item.Caption;
					frmBase.MdiParent = this;
					frmBase.Show();
				}
			}

			//hungdv added -- Cho phép tạo nhiều báo giá cùng thời điểm
			if (found && mdiId == 24)
			{
				FrmMdiBase frmBase = GetMdiForm(mdiId);
				if (frmBase != null)
				{
					frmBase.Text = item.Caption;
					frmBase.MdiParent = this;
					frmBase.Show();
				}
			}
		}

		/// <summary>
		/// 
		/// </summary>
		private void SetMenuAuth()
		{
			if (!SystemConfig.Instance.AuthenticateOn)
				return;
			FunctionCollection lstFnc = MasterInit.Instance.ListFunction;
			foreach (BarItem barItem in barManager.Items)
			{
				if (barItem is BarSubItem)
				{
					BarSubItem barSubItem = (BarSubItem)barItem;
					object tag = barSubItem.Tag;
					if (tag != null && !tag.ToString().Equals(""))
					{
						string functionCode = Convert.ToString(tag);
                        if (!lstFnc.ContainsFunctionCode(functionCode))
                            //barSubItem.Enabled = false;
                            barSubItem.Visibility = BarItemVisibility.Never;
					}
					//sub-menu
					foreach (LinkPersistInfo linkInfo in barSubItem.LinksPersistInfo)
					{
						if (linkInfo.Item is MyBarButtonItem)
						{
							MyBarButtonItem myBarBtn = (MyBarButtonItem)linkInfo.Item;
							object subTag = myBarBtn.Tag;
							if (subTag != null && !subTag.ToString().Equals(""))
							{
								string functionCode = Convert.ToString(subTag);
                                if (!lstFnc.ContainsFunctionCode(functionCode))
                                    //myBarBtn.Enabled = false;
                                    myBarBtn.Visibility = BarItemVisibility.Never;
							}
						}
					}
				}
			}
		}

		/// <summary>
		/// 
		/// </summary>
		/// <param name="id"></param>
		/// <returns></returns>
		public FrmMdiBase GetMdiForm(int id)
		{
			FrmMdiBase frmBase = null;
			switch (id)
			{
				#region MDI AUTH
				case MDI_ID_AUTH_MNG_USER:
					frmBase = new FrmMngUser();
					break;
				case MDI_ID_AUTH_MNG_GROUP:
					frmBase = new FrmMngGroup();
					break;
				case MDI_ID_AUTH_MAP_USER_GROUP:
					frmBase = new FrmMapGrpUser();
					break;
				case MDI_ID_AUTH_MAP_GROUP_FUNCTION:
					frmBase = new FrmMapGrpObj();
					break;

				#endregion
                //////////////////////////////////////////////////////////////////////////
                #region Admin

				//case MDI_ID_ADM_IMPORT_EXCEL:
				//    frmBase = new Admin.FrmImportExcel();
				//    break;

				#region Water
				case MDI_ID_WATER_CUSTOMER:
					frmBase = new Water.FrmCustomerList();
					break;
				case MDI_ID_WATER_STOCKOUT:
					frmBase = new Water.FrmWaterStockOut();
					break;
				case MDI_ID_WATER_STOCKOUT_LIST:
					frmBase = new Water.FrmWaterStockOutList();
					break;
				case MDI_ID_WATER_UPDATE_PRICE:
					frmBase = new Water.FrmUpdatePrice();
					break;
				case MDI_ID_WATER_REPORT_STOCKOUT:
					frmBase = new Water.FrmRptViewReportByConditionTotalStockOut();
					break;
				//case MDI_ID_WATER_REPORT_TOTAL_STOCKOUT:
				//    frmBase = new Water.FrmReportTotal();
				//    break;
				//case MDI_ID_WATER_MNG_BILL:
				//    frmBase = new Water.FrmWaterBill();
				//    break;
				case MDI_ID_WATER_MNG_RECEIPT:
					frmBase = new Water.FrmWaterReceipt();
					break;
				case MDI_ID_WATER_MNG_STOCKOUT:
					frmBase = new Water.FrmSumTotal();
					break;
				//case MDI_ID_WATER_REPORT_TOTAL_DEBIT:
				//    frmBase = new Water.FrmReportSumCustomerDebit();
				//    break;

				//case MDI_ID_WATER_REPORT_REPORT_SUM:
				//    frmBase = new Water.FrmReportSum();
				//    break;
				//case MDI_ID_AUTH_BACKUP_DATA:
				//    frmBase = new DBBackup.FrmBackUpRestore(BackUpType.BackUp);
				//    break;
				//case MDI_ID_AUTH_RESTORE_DATA:
				//    frmBase = new DBBackup.FrmBackUpRestore(BackUpType.Restore);
				//    break;
				#endregion
				#endregion

			}
			if (frmBase != null)
				frmBase.MdiId = id;
			return frmBase;
		}
		private void FrmMain_Load(object sender, EventArgs e)
		{
			this.BackgroundImage = EmbeddedRes.Instance.BimapLogo;
			this.BackgroundImageLayout = ImageLayout.Center;
		}
	}
}
